package com.cryptape.cita_wallet.activity.cloudminer;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.MapUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.cryptape.cita_wallet.CytonApplication;
import com.cryptape.cita_wallet.activity.NewActivateActivity;
import com.cryptape.cita_wallet.activity.cloudminer.MinerIncomeActivity;
import com.cryptape.cita_wallet.activity.NBaseActivity;
import com.cryptape.cita_wallet.activity.transfer.TransferActivity;
import com.cryptape.cita_wallet.adapter.CloudMinerAdapter;
import com.cryptape.cita_wallet.bean.CloudMinerBean;
import com.cryptape.cita_wallet.bean.CouldOrderBean;
import com.cryptape.cita_wallet.bean.VStimulateBean;
import com.cryptape.cita_wallet.constant.ConstantUtil;
import com.cryptape.cita_wallet.item.Token;
import com.cryptape.cita_wallet.net.callback.CommonCallback;
import com.cryptape.cita_wallet.net.request.HttpRequestMode;
import com.cryptape.cita_wallet.service.http.TokenService;
import com.cryptape.cita_wallet.util.RSAUtils;
import com.cryptape.cita_wallet.util.db.DBWalletUtil;
import com.cryptape.cita_wallet.view.SpacesItemDecoration;
import com.cryptape.cita_wallet.view.TitleBar;
import com.google.gson.Gson;
import com.mangoworld.android.R;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import rx.Subscriber;

/**
 * Created by dengming on 23/2/20.
 */
public class CloudMinerActivity extends NBaseActivity {

    private RecyclerView recyclerView;
    private CloudMinerAdapter minerAdapter;
    private TitleBar mTitle;
    private List<CloudMinerBean> minerBeans = new ArrayList<>();

    @Override
    protected int getContentLayout() {
        return R.layout.activity_cloud_miner_new;
    }

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.clound_miner_recycler);
        mTitle = findViewById(R.id.title_bar);
        initAdapter();
//        mTitle.setOnRightClickListener(new TitleBar.OnRightClickListener() {
//            @Override
//            public void onRightClick() {
//                startActivity(new Intent(CloudMinerActivity.this, MyMinerActivity.class));
//            }
//        });
//        mTitle.mTitleBarRightDefaultView.setCompoundDrawables(null, null, null, null);
    }

    @Override
    protected void initData() {
        initMinerData();
    }

    @Override
    protected void initAction() {

    }

    private void initMinerData() {
        String address = DBWalletUtil.getCurrentWallet(CytonApplication.getInstance()).address;
        try {
            Map params = MapUtils.newHashMap();
            params.put("address", address);
            String jsonData = GsonUtils.toJson(params);
            String content = RSAUtils.encrypt(jsonData);
            HttpRequestMode.getCouldOrder(CloudMinerActivity.this, content, true, new CommonCallback() {
                @Override
                public void onSuccess(Object data, String successMsg, int responseSuccessCode) {
                    minerBeans.clear();
                    if (data != null) {
                        List<CouldOrderBean> couldOrderBeanList = (List<CouldOrderBean>) data;
                        if (ObjectUtils.isNotEmpty(couldOrderBeanList)) {
                            for (int i = 0; i < couldOrderBeanList.size(); i++) {
//                                BigDecimal bdQuota;
//                                BigDecimal bdOpenQuota;
//                                String mQuota = couldOrderBean.getQuota();
//                                if (ObjectUtils.isEmpty(mQuota)) {
//                                    bdQuota = BigDecimal.ZERO;
//                                } else {
//                                    bdQuota = new BigDecimal(mQuota);
//                                }
//                                String mOpenQuota = couldOrderBean.getOpenQuota();
//                                if (ObjectUtils.isEmpty(mOpenQuota)) {
//                                    bdOpenQuota = BigDecimal.ZERO;
//                                } else {
//                                    bdOpenQuota = new BigDecimal(mQuota);
//                                }
//                                bdQuota.subtract(bdOpenQuota);
                                CouldOrderBean couldOrderBean = couldOrderBeanList.get(i);
                                CloudMinerBean bean = new CloudMinerBean();
                                bean.setId(i);
                                bean.setPower(couldOrderBean.getCalPower());
                                bean.setSurplus(couldOrderBean.getSurplus());
                                bean.setPrice(couldOrderBean.getQuota());
                                bean.setType(couldOrderBean.getType());
                                bean.setStatus(couldOrderBean.getStatus());
                                minerBeans.add(bean);
                            }
                        }
                    }
                    minerAdapter.notifyDataSetChanged();
                }

                @Override
                public void onFailure(String msg, int responseFailCode) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        initMinerData();
    }

    private void initAdapter() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

//        minerBeans.add(new CloudMinerBean());
        recyclerView.addItemDecoration(new SpacesItemDecoration(15));
        minerAdapter = new CloudMinerAdapter(this, minerBeans);
        recyclerView.setAdapter(minerAdapter);

        minerAdapter.setOnItemClickListener((view, position) -> {
            CloudMinerBean cloudMinerBean = minerBeans.get(position);
            if (cloudMinerBean.getType() == 0) {//未支付
                Token token = new Token(ConstantUtil.ETH_MAINNET, ConstantUtil.HMIO_TOKEN_SYMBOL, ConstantUtil.ETHEREUM_MAIN_ID);
                Intent intent = new Intent(CloudMinerActivity.this, MinerPayActivity.class);
                token.setContractAddress(ConstantUtil.getHToken());// "0xb2054aDd212F19581aEB195FDa7644471a996134";
                token.decimals = 18;
                intent.putExtra(TransferActivity.EXTRA_TOKEN, token);
                intent.putExtra(TransferActivity.EXTRA_ADDRESS, ConstantUtil.getMinerSellContract());
                intent.putExtra(MinerPayActivity.class.getSimpleName(), minerBeans.get(position));
                startActivity(intent);
            } else {
                Intent intent = new Intent(CloudMinerActivity.this, MinerIncomeActivity.class);
                intent.putExtra("isVStimulate", false);
                startActivity(intent);
            }
        });
    }

}
